package com.ljkj.zjf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ljkj.access.Tlog;
import com.ljkj.access.Tuser;
import com.ljkj.common.db.dao.ZjfGoodsClassDAO;
import com.ljkj.common.db.dao.ZjfLabelDAO;
import com.ljkj.common.db.domain.ZjfGoodsClass;
import com.ljkj.common.db.domain.ZjfGoodsType;
import com.ljkj.common.db.domain.ZjfLabel;
import com.ljkj.common.db.util.DateTime;
import com.ljkj.common.db.util.RandomCode;

public class ZjfLabelService {
	
	private ZjfLabelDAO zjfLabelDAO;
	private TLogService tLogService;

	/**
	 * 获取标签列表
	 * @param params
	 * @param currentPage
	 * @param pageSize
	 * @param sortField
	 * @param sortOrder
	 * @return
	 */
	public String getZjfLabelsByProperties(String params,int currentPage,int pageSize,String sortField,String sortOrder){
		
		Class[] pojos = {ZjfLabel.class};
		String[] propertyNames=null;
		Object[] values=null;
		String[] likePropertyNames = null;
		Object[] likevalues= null;
		String[] betweenPropertyNames=null;
		Object[][]  betweenvalues=null;
		String[] orderByPropertyNames=null;
		Object[] orderByValues=null;
		
		if(sortField != null){
			orderByPropertyNames = new String[]{sortField};
			orderByValues =  new String[]{sortOrder};
		}

		int count = this.zjfLabelDAO.getCount(pojos, propertyNames, values,likePropertyNames, likevalues, betweenPropertyNames,betweenvalues);
		this.zjfLabelDAO.setPageMaxCount(pageSize);
		List<ZjfLabel> zjfLabels = this.zjfLabelDAO.findKnowByProperties(pojos, propertyNames, values, likePropertyNames, likevalues, betweenPropertyNames, betweenvalues, orderByPropertyNames, orderByValues, currentPage);
		
		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(ZjfLabel zjfLabel : zjfLabels){
			
			Map<String,Object> entry_map = new HashMap<String,Object>();
			entry_map.put("labelId",zjfLabel.getLabelId());   
			entry_map.put("labelName",zjfLabel.getLabelName());
			entry_map.put("labelImg",zjfLabel.getLabelImg());
			entry_map.put("createTime",zjfLabel.getCreateTime());
			entry_map.put("createUser", zjfLabel.getCreateUser());
			entry_map.put("updateTime", zjfLabel.getUpdateTime());
			entry_map.put("updateUser", zjfLabel.getUpdateUser());
			entry_map.put("disabledState", zjfLabel.getDisabledState());
			
			entry_list.add(entry_map);
		}
		
		HashMap<String,Object> result_map = new HashMap<String, Object>();
		result_map.put("data", entry_list);
		result_map.put("total", count);
		
        return JSON.toJSONString(result_map);
	}
	
	public String getZjfLabelsForBless(){

		List<ZjfLabel> zjfLabels = this.zjfLabelDAO.findByProperty("disabledState", 0);
		
		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(ZjfLabel zjfLabel : zjfLabels){
			
			Map<String,Object> entry_map = new HashMap<String,Object>();
			entry_map.put("labelId",zjfLabel.getLabelId());   
			entry_map.put("labelName",zjfLabel.getLabelName());
			
			entry_list.add(entry_map);
		}
		
        return JSON.toJSONString(entry_list);
	}
	
	
	
	/**
	 * 新增或更新标签
	 * @param zjfGoodsClass
	 * @return
	 */
	public String addOrEditZjfLabelInfo(ZjfLabel zjfLabel,Tuser tuser){
		
		Tlog tlog = new Tlog();
		
		Map<String,String> result_map = new HashMap<String, String>();
		if(zjfLabel.getLabelId() == null){
			
			
			tlog.setOpType(1);
			tlog.setContent("新增分类："+ zjfLabel.getLabelName());
			zjfLabel.setLabelId(RandomCode.makeCode());
			zjfLabel.setCreateTime(DateTime.getDateTime());
			zjfLabel.setCreateUser(tuser.getLoginName());
			if(this.saveNewZjfLabel(zjfLabel)){
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				result_map.put("err_msg", "新增成功。");
				result_map.put("err_code", "0");
			}else{
				result_map.put("err_msg", "新增失败。");
				result_map.put("err_code", "1");
			}
			
		}else{
			try{
				
				if(zjfLabel.getDisabledState()!=null && zjfLabel.getDisabledState() == 1){
					tlog.setOpType(0);
					tlog.setContent("标签状态："+zjfLabel.getLabelName()+" 变为不可用");
				}else{
					tlog.setOpType(1);
					tlog.setContent("修改标签："+zjfLabel.getLabelName());
				}
				
				zjfLabel.setUpdateTime(DateTime.getDateTime());
				zjfLabel.setUpdateUser(tuser.getLoginName());
				this.zjfLabelDAO.update(zjfLabel);
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				
				result_map.put("err_msg", "更新成功。");
				result_map.put("err_code", "0");
			}catch(Exception ex){
				result_map.put("err_msg", "更新失败。");
				result_map.put("err_code", "1");
			}
		}
		
		return JSON.toJSONString(result_map);
	}
	
	public boolean saveNewZjfLabel(ZjfLabel zjfLabel){
		try{
			this.zjfLabelDAO.save(zjfLabel);
			return true;
		}catch(Exception ex){
			return false;
		}
	}
	
	public ZjfLabel getZjfLabelById(String labelId){
		return this.zjfLabelDAO.findById(labelId);
	}

	
	
	//注入DAO
	public void settLogService(TLogService tLogService) {
		this.tLogService = tLogService;
	}
	public void setZjfLabelDAO(ZjfLabelDAO zjfLabelDAO) {
		this.zjfLabelDAO = zjfLabelDAO;
	}
}
